package com.xxx.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.xxx.common.dto.Pagination;
import com.xxx.common.utils.DateUtil;
import com.xxx.model.FamilyHistory;

@Repository
public class FamilyHistoryDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 保存
	 * @param familyHistory
	 * @return id
	 */
	public long save(FamilyHistory familyHistory){
		String sql = "insert into t_family_history(id,dossierid,relatives,age,medicalhistory,recordstatus,createtime,updatetime) values (?,?,?,?,?,1,?,?)";
		Object obj[] = {familyHistory.getId(),familyHistory.getDossierid(),familyHistory.getRelatives(),familyHistory.getAge(),familyHistory.getMedicalhistory(),familyHistory.getCreatetime(),familyHistory.getUpdatetime()};
		int ret = jdbcTemplate.update(sql, obj);
		if (ret > 0) {
			return familyHistory.getId();
		}
		return 0;
	}
	
	/**
	 * 修改
	 * @param familyHistory
	 * @return id
	 */
	public long update(FamilyHistory familyHistory){
		String sql = "update t_family_history set dossierid=?,relatives=?,age=?,medicalhistory=?,updatetime=? where id=?";
		Object obj[] = {familyHistory.getDossierid(),familyHistory.getRelatives(),familyHistory.getAge(),familyHistory.getMedicalhistory(),familyHistory.getUpdatetime(), familyHistory.getId()};
		int ret = jdbcTemplate.update(sql, obj);
		if (ret > 0) {
			return familyHistory.getId();
		}
		return 0;
	}
	
	public int batchSave(List<FamilyHistory> list){
		String sql = "insert into t_family_history(id,dossierid,relatives,age,medicalhistory,recordstatus,createtime,updatetime) values (?,?,?,?,?,1,?,?)";
		List<Object[]> objList = new ArrayList<Object[]>();
		for (FamilyHistory familyHistory : list) {
			Object obj[] = {familyHistory.getId(),familyHistory.getDossierid(),familyHistory.getRelatives(),familyHistory.getAge(),familyHistory.getMedicalhistory(),familyHistory.getCreatetime(),familyHistory.getUpdatetime()};
			objList.add(obj);
		}
		if (!objList.isEmpty()) {
			int[] ret = jdbcTemplate.batchUpdate(sql, objList);
			if (ret.length > 0) {
				return ret.length;
			}
		}
		return 0;
	}
	
	/**
	 * 逻辑删除
	 * @param id
	 * @return true/false
	 */
	public boolean delete(long id){
		String sql = "update t_family_history set recordstatus=0,updatetime=? where id=?";
		Object obj[] = {DateUtil.dateToLong(),id};
		int ret = jdbcTemplate.update(sql, obj);
		return ret > 0;
	}
	
	public void deleteByDossierId(long dossierId) {
		String sql = "update t_family_history set recordstatus=0,updatetime=? where dossierid=?";
		Object obj[] = {DateUtil.dateToLong(),dossierId};
		jdbcTemplate.update(sql, obj);
	}
	
	/**
	 * 获取一个
	 * @param id
	 * @return FamilyHistory
	 */	
	public FamilyHistory getEntity(long id){
		String sql = "select id,dossierid,relatives,age,medicalhistory,createtime,updatetime from t_family_history where recordstatus=1 and id=?";
		final FamilyHistory familyHistory = new FamilyHistory();
		jdbcTemplate.query(sql, new Object[]{id},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				getResultSet(rs, familyHistory);
            }
		});
		return familyHistory;
	}

	/**
	 * 获取list
	 * @param whereMap
	 * @return list
	 */		
	public List<FamilyHistory> getList(Map<String, Object> whereMap){
		final List<FamilyHistory> list = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,dossierid,relatives,age,medicalhistory,createtime,updatetime from t_family_history where recordstatus=1 ");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		jdbcTemplate.query(sql.toString(), obj ,new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				list.add(getResultSet(rs, new FamilyHistory()));
            }
		});
		return list;
	}

	/**
	 * 获取list,分页
	 * @param whereMap
	 * @param pagination
	 * @return list
	 */			
	public List<FamilyHistory> getList(Map<String, Object> whereMap,Pagination pagination){
		final List<FamilyHistory> list = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,dossierid,relatives,age,medicalhistory,createtime,updatetime from t_family_history where recordstatus=1 ");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		sql.append(" order by " + pagination.getOrder());
		sql.append(" limit " + pagination.getPage() + "," + pagination.getPageSize());
		jdbcTemplate.query(sql.toString(), obj ,new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				list.add(getResultSet(rs, new FamilyHistory()));
			}
		});
		return list;
	}

	/**
	 * 获取个数
	 * @param whereMap
	 * @return int
	 */		
	public int getCount(Map<String, Object> whereMap){
		StringBuffer sql = new StringBuffer("select count(1) from t_family_history where recordstatus=1");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		Integer count = jdbcTemplate.queryForObject(sql.toString(), obj, Integer.class);
		if(count != null){
			return count;
		}
		return 0;
	}
	
	/**
	 * 获取条件
	 * @param whereMap
	 * @param sql
	 * @param whereObj
	 * @return sql
	 */	
	private StringBuffer getCondition(Map<String, Object> whereMap,StringBuffer sql,List<Object> whereObj){
		if (whereMap.containsKey("dossierid")) {
			long dossierid = (long) whereMap.get("dossierid");
			sql.append(" and dossierid=?");
			whereObj.add(dossierid);
		}
		if (whereMap.containsKey("relatives")) {
			String relatives = (String) whereMap.get("relatives");
			sql.append(" and relatives=?");
			whereObj.add(relatives);
		}
		if (whereMap.containsKey("age")) {
			int age = (int) whereMap.get("age");
			sql.append(" and age=?");
			whereObj.add(age);
		}
		if (whereMap.containsKey("medicalhistory")) {
			String medicalhistory = (String) whereMap.get("medicalhistory");
			sql.append(" and medicalhistory=?");
			whereObj.add(medicalhistory);
		}
		return sql;
	}
	
	/**
	 * 获取结果集
	 * @param whereMap
	 * @param familyHistory
	 * @return familyHistory
	 */	
	private FamilyHistory getResultSet(ResultSet rs,FamilyHistory familyHistory) throws SQLException{
		familyHistory.setId(rs.getLong("id"));
		familyHistory.setDossierid(rs.getLong("dossierid"));
		familyHistory.setRelatives(rs.getString("relatives"));
		familyHistory.setAge(rs.getInt("age"));
		familyHistory.setMedicalhistory(rs.getString("medicalhistory"));
		familyHistory.setCreatetime(rs.getLong("createtime"));
		familyHistory.setUpdatetime(rs.getLong("updatetime"));
		return familyHistory;
	}


}